﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using umbraco.cms.presentation.Trees;
using System.Data.SqlClient;
using System.Collections;

namespace MailQueueUmbracoAdmin
{
    public class MailQueueAdminTree : BaseTree
    {
        public MailQueueAdminTree(string application) : base(application) {}
        private static Hashtable labels;

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
        //    rootNode.Icon = FolderIcon;
        //    rootNode.OpenIcon = FolderIconOpen;
        //    rootNode.NodeType = "init" + TreeAlias;
        //    rootNode.NodeID = "init";
        }

        public override void Render(ref XmlTree tree)
        {
            string[] Items = new string[] { "All", "Pending", "OK", "Warning", "Error", "Labels"};
            string[] Icons = new string[] { "Database16.png", "1.png", "2.png", "4.png", "8.png", "folder.gif" };
            string[] IconsOpen = new string[] { "Database16.png", "1.png", "2.png", "4.png", "8.png", "folder_o.gif" };

            if (this.id == -1)
            {
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "10000";
                xNode.Text = Items[0];
                xNode.Action = "javascript:openMailQueueLog('','');";
                xNode.Icon = Icons[0];
                xNode.OpenIcon = IconsOpen[0];
                xNode.HasChildren = true;
                xNode.Source = this.GetTreeServiceUrl("10000");
                tree.Add(xNode);

                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "10001";
                xNode.Text = Items[5];
                //xNode.Action = "javascript:openMailQueueLog('','');";
                xNode.Icon = Icons[5];
                xNode.OpenIcon = IconsOpen[5];
                xNode.HasChildren = true;
                xNode.Source = this.GetTreeServiceUrl("10001");
                tree.Add(xNode);
            }

            if (this.id == 10000)
            {
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "20000";
                xNode.Text = Items[1];
                xNode.Action = "javascript:openMailQueueLog('','1');";
                xNode.Icon = Icons[1];
                xNode.OpenIcon = IconsOpen[1];
                tree.Add(xNode);

                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "20001";
                xNode.Text = Items[2];
                xNode.Action = "javascript:openMailQueueLog('','2');";
                xNode.Icon = Icons[2];
                xNode.OpenIcon = IconsOpen[2];
                tree.Add(xNode);

                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "20002";
                xNode.Text = Items[3];
                xNode.Action = "javascript:openMailQueueLog('','4');";
                xNode.Icon = Icons[3];
                xNode.OpenIcon = IconsOpen[3];
                tree.Add(xNode);

                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = "20003";
                xNode.Text = Items[4];
                xNode.Action = "javascript:openMailQueueLog('','8');";
                xNode.Icon = Icons[4];
                xNode.OpenIcon = IconsOpen[4];
                tree.Add(xNode);
            }

            if (this.id == 10001)
            {
                GetLabels();

                foreach (int key in labels.Keys)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = key.ToString();
                    xNode.Text = (string)labels[key];
                    xNode.Action = "javascript:openMailQueueLog('" + (string)labels[key] + "', '');";
                    xNode.Icon = Icons[0];
                    xNode.OpenIcon = IconsOpen[0];
                    xNode.HasChildren = true;
                    xNode.Source = this.GetTreeServiceUrl(key.ToString());
                    tree.Add(xNode);
                }
            }

            if (this.id > 30000)
            {
                int lId = this.id + 20;
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.NodeID = lId.ToString();
                xNode.Text = Items[1];
                xNode.Action = "javascript:openMailQueueLog('" + (string)labels[this.id] + "','1');";
                xNode.Icon = Icons[1];
                xNode.OpenIcon = IconsOpen[1];
                tree.Add(xNode);

                lId = this.id + 40;
                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = lId.ToString();
                xNode.Text = Items[2];
                xNode.Action = "javascript:openMailQueueLog('" + (string)labels[this.id] + "','2');";
                xNode.Icon = Icons[2];
                xNode.OpenIcon = IconsOpen[2];
                tree.Add(xNode);

                lId = this.id + 60;
                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = lId.ToString();
                xNode.Text = Items[3];
                xNode.Action = "javascript:openMailQueueLog('" + (string)labels[this.id] + "','4');";
                xNode.Icon = Icons[3];
                xNode.OpenIcon = IconsOpen[3];
                tree.Add(xNode);

                lId = this.id + 80;
                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = lId.ToString();
                xNode.Text = Items[4];
                xNode.Action = "javascript:openMailQueueLog('" + (string)labels[this.id] + "','8');";
                xNode.Icon = Icons[4];
                xNode.OpenIcon = IconsOpen[4];
                tree.Add(xNode);
            }

            
        }

        public override void RenderJS(ref System.Text.StringBuilder Javascript)
        {
            Javascript.Append(
                @"
                function openMailQueueLog(label, status) {
                    parent.right.document.location.href = 'Badcompany.Mailqueue/MailQueueAdmin.aspx?label=' + label + '&status=' + status;
                }");
        }

        private static void GetLabels()
        {
            string sqlCmdText = "SELECT DISTINCT Label FROM MailQueue ORDER BY LABEL DESC";

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlCmdText;
            cmd.CommandType = System.Data.CommandType.Text;
            SqlDataReader dr = DbHelper.DbHelper.ExecuteSqlReader(cmd, System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString);

            labels = new Hashtable();
            int lId = 30001;
            using (dr)
            {
                while (dr.Read())
                {
                    string lbl = String.Empty;
                    if (dr.IsDBNull(0))
                        lbl = "[No label]";
                    else
                        lbl = dr.GetString(0);

                    if (lbl.Length == 0)
                        lbl = "[No label]";

                    labels.Add(lId, lbl);
                    lId++;
                }
            }

            dr.Close();
        }
    }
}
